/*
* @Author: jack.lu
* @Date: 2020/5/13
* @Last Modified by: jack.lu
* @Last Modified time: 2020/5/13 5:52 下午
*/


import GoEasy from "goeasy";
import {Message, AudioMessage, MessageType,} from "./Message";
import restapi from "./restapi";

function DemoService() {
    this.messages = [];
    this.initGoEasy();
}

/**
 * 初始化
 */
DemoService.prototype.initGoEasy = function () {
    let self = this;
    this.goeasy = GoEasy({
        host: 'hangzhou.goeasy.io',
        appkey: '您的appkey',
        onConnected: function () {
            console.log("GoEasy connect successfully.");
            let message = new Message(MessageType.TEXT, "连接成功");
            self.messages.unshift(message);
        },
        onDisconnected: function () {
            console.log("GoEasy disconnected.")
            let message = new Message(MessageType.TEXT, "已断开连接");
            self.messages.unshift(message);
        },
        onConnectFailed: function (error) {
            console.log(error)
            console.log('GoEasy连接失败，请确认app.vue文件中appkey和host配置正确')
        }
    });
    this.listenNewMessage();
};

/**
 * 订阅消息
 */
DemoService.prototype.listenNewMessage = function () {
    let self = this;
    this.goeasy.subscribe({
        channel: "Multimedia_channel", //你的通道名称
        onMessage: function (message) {
            let msgContent = JSON.parse(message.content);
            self.messages.unshift(msgContent)
        },
        onSuccess: function () {
            let message = new Message(MessageType.TEXT, "订阅成功");
            self.messages.unshift(message);
        },
        onConnectFailed: function () {
            let message = new Message(MessageType.TEXT, "订阅失败");
            self.messages.unshift(message);
        }
    });
};

/**
 * 发送文本消息
 * @param type
 * @param content
 */
DemoService.prototype.sendTextMessage = function (type,content) {
    let message = new Message(type, content)
    this.publish(message);
};

/**
 * 发送文件消息
 * @param type
 * @param content
 * @returns {string|Q.Promise<T>|Promise<Cancel>|Promise<any>}
 */
DemoService.prototype.sendFileMessage = function (type,content) {
    let uploadResult = restapi.uploadFile(content),
        message = new Message(type, uploadResult.url)
    uploadResult.promise.then(() => {
        this.publish(message);
    },() =>  {
        let error = new Message(MessageType.TEXT, "文件上传失败.");
        this.messages.unshift(error)
    });
    return uploadResult.promise;
};

DemoService.prototype.sendAudio = function (audioFile, duration) {
    let uploadResult = restapi.uploadFile(audioFile);
    let message = new AudioMessage(MessageType.AUDIO, uploadResult.url, duration);
    uploadResult.promise.then(() => {
        this.publish(message);
    },() =>  {
        let error = new Message(MessageType.TEXT, "文件上传失败.");
        this.messages.unshift(error)
    });
    return uploadResult.promise;
};



/**
 * 发布消息
 * @param message
 */
DemoService.prototype.publish = function (message) {
    this.goeasy.publish({
        channel: "Multimedia_channel",
        message: JSON.stringify(message),
        onSuccess: function () {
            console.log("send message success");
        },
        onFailed: function (error) {
            console.log("send message error: ", JSON.stringify(error));
        }
    });
};

export default DemoService;